//
//  GoodsCell.m
//  WisdomZhuHai
//
//  Created by Admin on 14-10-28.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "GoodsCell.h"

const static CGFloat MARGIN_X = 8;
const static CGFloat OFFSET_X = 8;
const static CGFloat OFFSET_Y = 6;
const static CGFloat INTERVAL_Y = 4;
const static CGSize TITLE_SIZE = {240, 28};
const static CGSize BOOK_BUTTON_SIZE = {55, 24};
const static CGFloat DETAIL_TEXT_HEIGHT = 20;
const static CGSize LINK_BUTTON_SIZE = {72,DETAIL_TEXT_HEIGHT};

@interface GoodsCell()

@end

@implementation GoodsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(MARGIN_X, 0, self.contentView.bounds.size.width - MARGIN_X*2, OFFSET_Y*2 + TITLE_SIZE.height)];
        titleView.tag = 1000;
        titleView.backgroundColor = [UIColor whiteColor];
        titleView.layer.borderWidth = 0.5;
        titleView.layer.borderColor = [UIColor grayColor].CGColor;
        [self.contentView addSubview:titleView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapTitle:)];
        [titleView addGestureRecognizer:tapGesture];
        
        UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(OFFSET_X, OFFSET_Y, TITLE_SIZE.width, TITLE_SIZE.height)];
        lbTitle.tag = 1001;
        lbTitle.font = [UIFont systemFontOfSize:16];
        lbTitle.backgroundColor = [UIColor clearColor];
        lbTitle.textColor = [UIColor blackColor];
        [titleView addSubview:lbTitle];
        
        UIButton *btnBook = [UIButton buttonWithType:UIButtonTypeCustom];
        btnBook.tag = 1002;
        btnBook.frame = CGRectMake(titleView.bounds.size.width - OFFSET_X - BOOK_BUTTON_SIZE.width, OFFSET_Y + + (TITLE_SIZE.height - BOOK_BUTTON_SIZE.height)/2, BOOK_BUTTON_SIZE.width, BOOK_BUTTON_SIZE.height);
        [btnBook setImage:[UIImage imageNamed:@"hotel/Schedule.png"] forState:UIControlStateNormal];
        [btnBook setImage:[UIImage imageNamed:@"hotel/Schedule2.png"] forState:UIControlStateHighlighted];
        [btnBook addTarget:self action:@selector(onClickBook:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:btnBook];
        
        UIView* detailView = [[UIView alloc] initWithFrame:CGRectMake(MARGIN_X+1, titleView.bounds.size.height, self.contentView.bounds.size.width-2*(MARGIN_X+1), 0)];
        detailView.tag = 1003;
        detailView.backgroundColor = [UIColor colorWithWhite:0.94 alpha:0.8];
        [self.contentView addSubview:detailView];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)onTapTitle:(UITapGestureRecognizer*)gesture
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showGoodsDetail:)]) {
        [self.delegate showGoodsDetail:self];
    }
}

- (void)onClickBook:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(bookGoods:)]) {
        [self.delegate bookGoods:self];
    }
}

- (void)onClickLink:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onClickLink:)]) {
        [self.delegate onClickLink:self];
    }
}

+ (CGFloat)getCellHeight:(NSArray*)goodsDetail ifShowDetail:(BOOL)ifShowDetail
{
    CGFloat height = OFFSET_Y*2 + TITLE_SIZE.height;
    if (ifShowDetail) {
        int rowCount = (goodsDetail.count + 1)/2 + 1;
        height += INTERVAL_Y + rowCount*(DETAIL_TEXT_HEIGHT+INTERVAL_Y);
    }
    return height;
}

- (void)setGoodsName:(NSString*)name
{
    UILabel *lb = (UILabel*)[self viewWithTag:1001];
    lb.text = name;
}

- (void)setGoodsDetail:(NSArray*)goodsDetail ifShowDetail:(BOOL)ifShowDetail
{
    UIView *detailView = [self viewWithTag:1003];
    CGRect frame = detailView.frame;
    
    for (UIView* view in detailView.subviews) {
        [view removeFromSuperview];
    }
    
    if (!ifShowDetail) {
        frame.size.height = 0;
        detailView.frame = frame;
        return;
    }
    
    int rowCount = (goodsDetail.count + 1)/2 + 1;
    frame.size.height = INTERVAL_Y + rowCount*(DETAIL_TEXT_HEIGHT+INTERVAL_Y);
    detailView.frame = frame;
    
    for (int i=0; i<goodsDetail.count; i++) {
        UILabel *lb = [self createDetailLabel];
        lb.text = goodsDetail[i];
        frame = lb.frame;
        frame.origin.y = INTERVAL_Y + (i/2)*(INTERVAL_Y + DETAIL_TEXT_HEIGHT);
        if (i%2 == 0) {
            frame.origin.x = OFFSET_X;
        }
        else {
            frame.origin.x = detailView.bounds.size.width - OFFSET_X - lb
            .bounds.size.width;
            lb.textAlignment = NSTextAlignmentRight;
        }
        lb.frame = frame;
        [detailView addSubview:lb];
    }
    
    UIButton *btnLink = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLink.frame = CGRectMake(detailView.bounds.size.width - LINK_BUTTON_SIZE.width, detailView.bounds.size.height - LINK_BUTTON_SIZE.height, LINK_BUTTON_SIZE.width, LINK_BUTTON_SIZE.height);
    [btnLink setTitle:@"图文详情" forState:UIControlStateNormal];
    [btnLink setTitleColor:[UIColor colorWithRed:1.0f green:0.1f blue:0.f alpha:1.f] forState:UIControlStateNormal];
    [btnLink addTarget:self action:@selector(onClickLink:) forControlEvents:UIControlEventTouchUpInside];
    btnLink.titleLabel.font = [UIFont systemFontOfSize:14];
    [detailView addSubview:btnLink];
}

#pragma mark - Private
- (UILabel*)createDetailLabel
{
    CGFloat width = self.contentView.bounds.size.width/2 - MARGIN_X - OFFSET_X;
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, DETAIL_TEXT_HEIGHT)];
    lb.backgroundColor = [UIColor clearColor];
    lb.font = [UIFont systemFontOfSize:14];
    lb.textColor = [UIColor darkGrayColor];
    return lb;
}

@end
